package com.ms.content.config;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Hashtable;
import java.util.Map;

@Component
public class TextER {
    // 生成二维码
    public void encode(String contents, int width, int height, String imgPath) {

        Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();

        // 指定纠错等级

        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

        // 指定编码格式

        hints.put(EncodeHintType.CHARACTER_SET, "UTF8");

        try {

            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);

            MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(imgPath));

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    // 二维码解析

    public String decode(String imgPath) {

        BufferedImage image = null;

        Result result = null;

        try {

            image = ImageIO.read(new File(imgPath));

            if (image == null) {

                System.out.println("the decode image may be not exit.");

            }

            LuminanceSource source = new BufferedImageLuminanceSource(image);

            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

            Map<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();

            hints.put(DecodeHintType.CHARACTER_SET, "UTF8");

            result = new MultiFormatReader().decode(bitmap, hints);

            return result.getText();

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

    // 测试代码

    public static void main( String[] args ){

        new TextER().encode("www.baidu.com", 500, 500, "E:\\资料coreJava\\1906a\\miaosha\\img\\1.png");

        System.out.println(new TextER().decode("E:\\资料coreJava\\1906a\\miaosha\\img\\1.png"));

    }
}
